package org.xt.module.api;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpSession;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.xt.constants.Const;
import org.xt.constants.Res;
import org.xt.entity.DataSummary;
import org.xt.entity.DataGJEHState;
import org.xt.entity.DataItem;
import org.xt.entity.DataMachine;
import org.xt.entity.DataTempputState;
import org.xt.entity.User;
import org.xt.service.DataSummaryService;
import org.xt.service.PermissionService;
@IocBean
@At("/api/dataSummary")
public class DataSummaryApiModule {
	@Inject
	private Dao dao;
	@Inject
	private DataSummaryService dataSummaryService;
	
	@At("/add")
	@Ok("json")
	public Res add(@Param("..")DataSummary data){
		try{
			if(dataSummaryService.add(data)){
				return Res.NEW().code(Res.SUCCESS).msg("添加OK").data(data);
			}else{
				return Res.NEW().code(Res.ERROR).msg("添加失败");
			}
		}catch(Exception e){
			if(e.getMessage().contains("Violation of PRIMARY KEY constraint")){
				return Res.NEW().code(Res.ERROR).msg("数据库已有此数据，切勿重复添加");
			}else{
				return Res.NEW().code(Res.ERROR).msg("系统异常，请重试");
			}
		}
	}
	
	@At("/in")
	@Ok("json")
	public Res in(@Param("..")DataSummary data, HttpSession session){
		User user = (User) session.getAttribute(Const.LOGIN_USER_SIGN);
		return dataSummaryService.update(data, user);
	}
	
	@At("/out")
	@Ok("json")
	public Res out(@Param("..")DataSummary data, HttpSession session){
		User user = (User) session.getAttribute(Const.LOGIN_USER_SIGN);
		data.setNum(-data.getNum());
		return dataSummaryService.update(data, user);
	}
	
	@At("/list")
	@Ok("json")
	public Res list(int machineId, @Param(value="table", df="tb_data_1fgjeh_details_auto")String table){
		return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.list(machineId, table));
	}
	@At("/list1")
	@Ok("json")
	public Res list1(int machineId, @Param(value="table", df="tb_data_1fgjeh_details_auto")String table){
		return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.list1(machineId, table));
	}
	@At("/list2")
	@Ok("json")
	public Res list2(String machine, String item, String gjehState, String tempputState
			, String liaohao){
		return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.list2(machine, item, gjehState, tempputState
				, liaohao));
	}
	@At("/xjxc")
	@Ok("json")
	public Res xjxc(String machine, String item, String gjehState, String tempputState
			, String liaohao){
		return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.xjxc(machine, item, gjehState, tempputState
				, liaohao));
	}
	@At("/list3")
	@Ok("json")
	public Res list3(){
		return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.list3());
	}
	
	@At("/list3_1")
	@Ok("json")
	public Res list3_1(String machine, String gjehState){
		return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.list3_1(machine, gjehState));
	}
	
	@At("/delete")
	@Ok("json")
	public Res delete(@Param("..")DataSummary data){
		if(dataSummaryService.delete(data.getDataMachineId(), data.getDataItemId(), data.getDataGJEHStateId(), data.getDataTempputStateId())){
			return Res.NEW().code(Res.SUCCESS).msg("删除成功");
		}else{
			return Res.NEW().code(Res.ERROR).msg("删除失败");
		}
	}
	
	@At("/findMachines")
	@Ok("json")
	public Res findMachines(HttpSession session){
		User user = (User) session.getAttribute(Const.LOGIN_USER_SIGN);
		if(user != null){
			return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.findMachines(user));
		}else{
			return Res.NEW().code(Res.ERROR).msg("没登陆");
		}
	}
	
	@At("/findMachinesByAuto")
	@Ok("json")
	public Res findMachinesByAuto(){
		return Res.NEW().code(Res.SUCCESS).msg("获取机种OK").data(dataSummaryService.findMachinesByAuto());
	}
	@At("/findMachinesByHM")
	@Ok("json")
	public Res findMachinesByHM(){
		return Res.NEW().code(Res.SUCCESS).msg("获取机种OK").data(dataSummaryService.findMachinesByHM());
	}
	@At("/findMachinesNoNeedLogin")
	@Ok("json")
	@Filters
	public Res findMachinesNoNeedLogin(){
		return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.findMachines());
	}
	
	@At("/findItems")
	@Ok("json")
	public Res findItems(int machineId, HttpSession session){
		User user = (User) session.getAttribute(Const.LOGIN_USER_SIGN);
		if(user != null){
			return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.findItems(machineId, user));
		}else{
			return Res.NEW().code(Res.ERROR).msg("没登陆");
		}
	}
	@At("/findItemsByAuto")
	@Ok("json")
	public Res findItemsByAuto(int machine){
		DataMachine ma = dao.fetch(DataMachine.class, machine);
		return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.findItemsByAuto(ma!=null?ma.getName():""));
	}
	@At("/findItemsByHM")
	@Ok("json")
	public Res findItemsByHM(){
		return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.findItemsByHM());
	}
	@At("/findGjehStates")
	@Ok("json")
	public Res findGjehStates(int machineId, int itemId, HttpSession session){
		User user = (User) session.getAttribute(Const.LOGIN_USER_SIGN);
		if(user != null){
			return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.findGjehStates(machineId, itemId, user));
		}else{
			return Res.NEW().code(Res.ERROR).msg("没登陆");
		}
	}
	@At("/findGjehStatesByAuto")
	@Ok("json")
	public Res findGjehStatesByAuto(int machine, int item){
		DataMachine ma = dao.fetch(DataMachine.class, machine);
		DataItem it = dao.fetch(DataItem.class, item);
		return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.findGjehStatesByAuto(
				ma!=null?ma.getName():""
				, it!=null?it.getName():""
				));
	}
	@At("/findGjehStatesByHM")
	@Ok("json")
	public Res findGjehStatesByHM(){
		return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.findGjehStatesByHM());
	}
	@At("/findTempputStates")
	@Ok("json")
	public Res findTempputStates(int machineId, int itemId, int gjehStateId, HttpSession session){
		User user = (User) session.getAttribute(Const.LOGIN_USER_SIGN);
		if(user != null){
			return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.findTempputStates(machineId, itemId, gjehStateId, user));
		}else{
			return Res.NEW().code(Res.ERROR).msg("没登陆");
		}
	}
	@At("/findTempputStatesByAuto")
	@Ok("json")
	public Res findTempputStatesByAuto(int machine, int item, int gjehState){
		DataMachine ma = dao.fetch(DataMachine.class, machine);
		DataItem it = dao.fetch(DataItem.class, item);
		DataGJEHState gj = dao.fetch(DataGJEHState.class, gjehState);
		return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.findTempputesByAuto(
				ma!=null?ma.getName():""
				, it!=null?it.getName():""
				, gj!=null?gj.getName():""
				));
	}
	@At("/findTempputStatesByHM")
	@Ok("json")
	public Res findTempputStatesByHM(){
		return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.findTempputesByHM());
	}
	@At("/findLiaohaosByAuto")
	@Ok("json")
	public Res findLiaohaosByAuto(int machine, int item, int gjehState, int tempputState){
		DataMachine ma = dao.fetch(DataMachine.class, machine);
		DataItem it = dao.fetch(DataItem.class, item);
		DataGJEHState gj = dao.fetch(DataGJEHState.class, gjehState);
		DataTempputState te = dao.fetch(DataTempputState.class, tempputState);
		return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.findLiaohaoByAuto(
				ma!=null?ma.getName():""
				, it!=null?it.getName():""
				, gj!=null?gj.getName():""
				, te!=null?te.getName():""
				));
	}
	@At("/findLiaohaosByHM")
	@Ok("json")
	public Res findLiaohaosByHM(){
		return Res.NEW().code(Res.SUCCESS).msg("ok").data(dataSummaryService.findLiaohaoByHM());
	}
	@At("/find1FGjeh")
	@Ok("json")
	public Res find1FGjeh(int machineId, int itemId, int gjehStateId, int tempputStateId){
		return Res.NEW().code(Res.SUCCESS).msg("ok").data(dao.fetchx(DataSummary.class, machineId, itemId, gjehStateId, tempputStateId));
	}
}
